<template>
  <transition name="fade">
    <div v-if="visible" class="remind-dialog-mask">
      <div class="remind-dialog">
        <div class="remind-dialog-header">
          <div class="remind-dialog-close-wrap">
            <button class="remind-dialog-close-combined" @click="handleClose" aria-label="关闭">
              <el-countdown
                v-if="showCountdown"
                :value="countdownTarget"
                format="ss秒后关闭"
                class="remind-dialog-countdown"
              />
              <span class="close-x">×</span>
            </button>
          </div>
        </div>
        <div class="remind-dialog-main" style="flex:1;min-height:0;">
          <div class="remind-dialog-avatar-wrap">
            <img class="remind-dialog-avatar" :src="avatarUrl" alt="avatar" />
          </div>
          <div class="remind-dialog-content">
            <div class="remind-content-main">
              <slot />
            </div>
            <div class="remind-content-footer">
              <div class="remind-footer-text">——您的康养管家温馨提醒</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup>
import { ref, watchEffect, onMounted, onBeforeUnmount, computed } from 'vue';
import { ElCountdown } from 'element-plus';

const props = defineProps({
  modelValue: Boolean,
  title: { type: String, default: '提醒' },
  duration: { type: Number, default: 20000 }, // 默认10秒自动关闭
});
console.log('RemindDialog props.modelValue:', props.modelValue);
const emit = defineEmits(['update:modelValue', 'close']);

const visible = ref(props.modelValue);
let timer = null;
const countdownSeconds = ref(0); // 改为倒计时秒数


const showCountdown = computed(() => props.duration > 0);

// 固定人像头像
const avatarUrl = '/static/images/butler.png';



function clearAllTimers() {
  if (timer) {
    clearTimeout(timer);
    timer = null;
  }
}

const countdownTarget = ref(Date.now());

function startCountdown() {
  clearAllTimers();
  if (props.duration > 0) {
    countdownTarget.value = Date.now() + props.duration;
    timer = setTimeout(() => {
      clearAllTimers();
      handleClose();
    }, props.duration);
  }
}

watchEffect(() => {
  visible.value = props.modelValue;
  if (props.modelValue) {
    startCountdown();
  } else {
    clearAllTimers();
  }
});

function handleClose() {
  visible.value = false;
  emit('update:modelValue', false);
  emit('close');
  clearAllTimers();
}

onMounted(() => {
  // 弹窗初始不显示，无需倒计时，弹窗显示时由watch处理
});
onBeforeUnmount(() => {
  clearAllTimers();
});
</script>

<style scoped>
.remind-dialog-mask {
  position: fixed;
  left: 0; top: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.25);
  z-index: 99999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.remind-dialog {
  background: rgba(216, 216, 216, 0.85);
  border-radius: 16px;
  /* min-width: 600px; */
  max-width: 1000px;
  min-height: 400px;
  max-height: 500px;
  padding: 20px 26px;
  box-shadow: 0 4px 32px #e3e7ed;
  color: #fff;
  display: flex;
  flex-direction: column;
}
.remind-dialog-header {
  position: relative;
  height: 32px;
  margin-bottom: 18px;
}
.remind-dialog-close-wrap {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
  align-items: center;
  z-index: 2;
}
/* 合并倒计时和关闭按钮样式 */
.remind-dialog-close-combined {
  background: #f5f5f5;
  border: none;
  color: #333;
  font-size: 28px;
  cursor: pointer;
  line-height: 1;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 18px;
  transition: background 0.2s;
  padding: 0 14px 0 12px;
}
.remind-dialog-close-combined:hover {
  background: #e6e6e6;
}
.remind-dialog-countdown {
  font-size: 28px;
  color: #666;
  background: transparent;
  border-radius: 8px;
  padding: 2px 2px 2px 2px;
  display: inline-block;
  min-width: 80px;
  margin-right: 0px;
}
.close-x {
  font-size: 32px;
  margin-left: 2px;
  color: #333;
}
.remind-dialog-main {
  display: flex;
  align-items: stretch;
  height: 100%;
}
.remind-dialog-avatar-wrap {
  /* height: 100%; */
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  background: transparent;
  padding-left: 0;
}
.remind-dialog-avatar {
  height: 100%;
  max-height: 360px; /* 限制过高 */
  min-width: 140px; /* 保持原来较宽外观，不再缩得过小 */
  object-fit: contain;
  background: transparent;
  border: none;
  display: block;
  margin: 0;
  margin-right: 6px; /* 与内容分隔，减小整体右移 */
}
.remind-dialog-content {
  font-size: 22px;
  color: #222;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  min-height: 200px;
  position: relative;
  padding-top: 0;
  padding-left: 4px; /* 再微调缩小 */
}
.remind-content-main {
  margin-top: 0;
  text-align: left;
}
.remind-content-footer {
  font-size: 22px;
  color: #222;
  text-align: right;
  margin-top: auto;
  padding-top: 16px;
  opacity: 0.85;
}
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.2s;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>